package com.bwie.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.bwie.user.util.JwtUtil;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author HouDing
 * @email 1398853436@qq.com
 * @date 2022-03-23 21:38:29
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Integer userId){
		UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }
    //验证原密码
    @RequestMapping("/check/{userId}")
    public R checkPassword(@PathVariable("userId")Integer userId,@RequestParam("oldPwd") String oldPwd){
		UserEntity user = userService.checkPassword(userId,oldPwd);
        return R.ok().put("user", user);
    }
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity userEntity){
		UserEntity user = userService.login(userEntity);
        if(user!=null) {
            String token = jwtUtil.createJWT(user.getUserId() + "", user.getUserNickName(), "user");
            return R.ok().put("token",token).put("user",user);
        }
        return R.error("手机号或密码错误");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
